<template>
  <div>
    <el-upload
      ref="upload_file"
      class="upload-demo"
      :action="uploadUrl"
      :data="uploadData"
      :on-preview="handlePreview"
      :on-remove="handleRemove"
      :limit="99999"
      multiple
      :before-upload="uploadBefore"
      :on-success="uploadSuccess"
    >
      <el-button slot="trigger" type="primary">
        <i class="el-icon-upload el-icon--right" style="color: white;"></i>
        <span class="mobileHide">上传文件</span>
      </el-button>
      <el-button class="el-icon-close" size="mini" plain circle style="margin-left: 5%;" hidden
                 v-show="close_list" @click="clear_list"></el-button>

    </el-upload>


  </div>

</template>
<script>
import Global from "../js/global";
import eventBus from "../js/eventBus"

export default {
  name: "UploadFile",
  props: ['user', 'token'],

  data() {
    return {
      close_list: false,
      uploadData: {
        token: '',
        fatherFileFolderId: '0'
      },
      uploadUrl: Global.SERVER_ADDRESS + '/file/uploadFile',
      parentFolderId: ''
    };
  },
  created() {
    eventBus.$on("FolderId", data => {
      this.uploadData.fatherFileFolderId = data;
    });
  },
  methods: {
    outLogin() {
      this.$cookies.remove("TOKEN")
      this.$router.push('/login');
    },
    handleSelect(key, keyPath) {
    },
    handleRemove(file, fileList) {
    },
    handlePreview(file) {
    },
    uploadBefore(file) {
      const isLt2M = file.size / 1024 / 1024 < 2 * 1024;
      if (!isLt2M) {
        this.$message.error('上传文件大小不能超过 2GB!');
        return false;
      }
      this.uploadData.token = this.token;
    },
    uploadSuccess(response, file, fileList) {
      const resp = response;
      if (resp.code === '200') {
        this.successNotification(resp.msg);
        eventBus.$emit("refresh", "do");
      } else {
        this.errorNotification(resp.msg);
      }
    },
    successNotification(msg) {
      this.close_list = true;
      this.$notify({
        title: '成功',
        message: msg,
        type: 'success'
      });
    },
    errorNotification(msg) {
      this.$notify.error({
        title: '失败',
        message: msg
      });
    },
    clear_list() {
      this.$refs.upload_file.clearFiles();
      this.close_list = false;
    },

  }
}
</script>

<style scoped>

</style>
